<template>
  <div class="login-container">
    <el-card class="login-card">
      <h2 class="login-title">智慧养老管理系统</h2>
      <el-form :model="loginForm" :rules="rules" ref="loginForm" label-width="20">
        <el-form-item label="用户名" prop="userName">
          <el-input v-model="loginForm.userName" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="loginForm.password" placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm">登录</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>
 
<script>

import axios from 'axios'
import { setToken } from '@/util/auth'
import { removeToken } from '@/util/auth'
import store from '@/store'
import {generateRoutes} from '../util/index'
export default {
  data () {
    return {
      loginForm: {
        userName: '',
        password: ''
      },
      rules: {
        userName: [
          { required: true, message: '请输入用户名!', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码!', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    submitForm () {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          //定义请求头
          const headers = {
              isToken: false
          }
          axios.post('/api/user/login', this.loginForm, {headers})
            .then(res => {
                    if (res.data.code == 200) {
                    setToken(res.data.data.token);
                    store.commit('SET_MENULIST', res.data.data.tbAdmin.menus);
                    generateRoutes();
                    this.$router.push("/").catch(()=>{});
                    sessionStorage.setItem("name",res.data.data.tbAdmin.realName)
                } else {
                    alert(res.data.message);  
                }
            })
        } else {
          console.log('error submit!')
          return false
        }
      })
    }
  }
}
</script>
 
<style>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-image: url('../assets/333.jpg');
  background-size: cover;
}
.login-card {
  width: 400px;
}
.login-title {
  text-align: center;
  margin-bottom: 20px;
}
</style>